package models

import (
	"fmt"
	"github.com/spf13/cast"
	"time"
)

type Gnss struct {
	Id                int64     `xorm:"not null pk autoincr comment('自增主键') INT(11)"`
	LearnStatusId     int       `xorm:"not null comment('教练车状态id外键关联dts_learn_status中的id') index INT(11)"`
	TerminalId        int       `xorm:"not null comment('终端设备id外键关联dts_terminal表中的id') index(inx_order) index INT(11)"`
	Alarmflag         float64   `xorm:"comment('报警标识  见表B.22') DOUBLE"`
	Latitude          float32   `xorm:"not null comment('纬度x/度，单位精确到小数点后6位') FLOAT(15,13)"`
	Longitude         float32   `xorm:"not null comment('经度y/度，单位精确到小数点后6位') FLOAT(16,13)"`
	Velocitycar       int       `xorm:"default 0 comment('车速表记录瞬时车速100m/h') SMALLINT(5)"`
	Velocitysatellite int       `xorm:"default 0 comment('卫星记录瞬时车速100m/h') SMALLINT(5)"`
	Oritentation      int       `xorm:"default 0 comment('车辆行驶朝向0-359, 0表示正北') SMALLINT(3)"`
	AddTime           time.Time `xorm:"not null comment('gnss信息添加时间') DATETIME"`
	Status            int       `xorm:"comment('状态   见表B.23状态位') SMALLINT(5)"`
	Gnssmileage       float32   `xorm:"comment('里程') FLOAT(12)"`
	Oil               float32   `xorm:"comment('油量') FLOAT(12)"`
	Heightsea         int       `xorm:"comment('海拔高度') INT(10)"`
	Enginespeed       int       `xorm:"comment('发动机转速') INT(11)"`
	Maxspeed          float32   `xorm:"default 0.000 comment('最大速度') FLOAT(12,3)"`
	Mileage           float32   `xorm:"comment('里程') index FLOAT(12,3)"`
	LearnrecordNumber string    `xorm:"comment('学时编号') index CHAR(26)"`
	Subject           string    `xorm:"comment('科目一二三四') ENUM('1','2','3','4')"`
	Type              string    `xorm:"default '0' comment('该段学时类型标识，0：练习，1：考试，2：实车教学,3:课堂教学,4:模拟器教学,5:远程教学') ENUM('0','1','2','3','4','5')"`
	ApprovalStatus    string    `xorm:"default '0' comment('本次学时提交运管审核状态，0:待审核，1:合格，2:不合格') ENUM('0','1','2')"`
	Reason            string    `xorm:"comment('审核合格不合格的原因') VARCHAR(255)"`
	RecordStatus      string    `xorm:"default '0' comment('学时状态(0  不正常记录  1  正常记录)') ENUM('0','1')"`
	Checkfence        string    `xorm:"comment('0代表 出围栏 1代表在围栏之内') ENUM('0','1')"`
	Datetime          time.Time `xorm:"index index(inx_order) DATETIME"`
	Image             *File     `xorm:"-" `
}

func (c *Gnss) GetByFilter(id string) (recordMileage float32, err error) {
	sql := fmt.Sprintf("SELECT sum(mileage) as mileage FROM `dts_gnss` where id in (SELECT gnss_id from dts_learn_record_gnss where learn_record_id=%s)", id)
	results, err := engine.QueryString(sql)
	if err != nil || len(results) == 0 {
		return 0, err
	}
	//err = engine.Table("dts_gnss").Select("sum(mileage)").Where("id in (?)", engine.Table("dts_learn_record_gnss").Select("gnss_id").Where("learn_record_id = ?", id).QueryExpr()).Find(&list)
	//if err = engine.Table("dts_gnss").And(filter).Find(&list); err != nil {
	//	return nil, err
	//}
	return cast.ToFloat32(results[0]["mileage"]), nil
}

func (g *Gnss) GetCoachGnss() (gnss Gnss, err error) {
	if _, err = engine.Table("dts_gnss").Desc("id").Limit(1).Get(&gnss); err != nil {
		return gnss, err
	}
	return gnss, nil
}

func (m *Gnss) GetGnssByRecordId(recordId int) (list []*Gnss, err error) {
	if recordId >= 26522343 {
		if err = engine.Table("dts_gnss").Cols("mileage,learnrecord_number").
			Where("id in (select gnss_id from dts_learn_record_gnss where learn_record_id=?)", recordId).Find(&list); err != nil {
			return nil, err
		}
	} else if recordId > 25812844 && recordId < 26522343 {
		if err = engine.Table("dts_gnss_2023").Cols("mileage,learnrecord_number").
			Where("id in (select gnss_id from dts_learn_record_gnss_2023 where learn_record_id=?)", recordId).Find(&list); err != nil {
			return nil, err
		}
	} else {
		if err = engine.Table("dts_gnss_2023").Cols("mileage,learnrecord_number").
			Where("id in (select gnss_id from dts_learn_record_gnss_bak where learn_record_id=?)", recordId).Find(&list); err != nil {
			return nil, err
		}
	}
	return list, nil
}
